type IncomeAndOutgo = {
  edgeId?: string;
  incomingEdgeId?: string;
  outgoingEdgeId?: string;
  angle: number;
};
/**
 * 获取输入、输出的角度
 * @param { string } nodeId 节点ID
 * @param { HTMLElement }  graph  x6画布元素
 */
const getIncomingToOutgoing = (nodeId: string, graph: any) => {
  // 输入的边
  const incoming = graph.getIncomingEdges(nodeId);
  // 输出的边
  const outgoing = graph.getOutgoingEdges(nodeId);
  // 处理输入的边的角度集合
  const incomingAngleList: IncomeAndOutgo[] = [];
  // 处理输入的边
  if (incoming) {
    incoming.forEach((item: any) => {
      // 获取边的终点和起点;
      const sourcePoint = item.getSourceNode()?.position(); // 获取起点位置
      const targetPoint = item.getTargetNode()?.position(); // 获取终点位置
      // 计算差值
      const deltaX = sourcePoint.x - targetPoint.x;
      const deltaY = sourcePoint.y - targetPoint.y;
      // 转换为度
      const angle = Math.atan2(deltaY, deltaX) * (180 / Math.PI);
      const angleTofixed = angle === 0 ? 0 : -angle.toFixed(1);
      incomingAngleList.push({
        edgeId: item.id,
        angle: Number(angleTofixed),
      });
    });
  }
  // 处理输出的边的角度集合
  const outgoingAngleList: IncomeAndOutgo[] = [];
  // 处理输出的边
  if (outgoing) {
    outgoing.forEach((item: any) => {
      // 获取边的终点和起点;
      const sourcePoint = item.getSourceNode()?.position(); // 获取起点位置
      const targetPoint = item.getTargetNode()?.position(); // 获取终点位置
      // 计算差值
      const deltaX = targetPoint.x - sourcePoint.x;
      const deltaY = targetPoint.y - sourcePoint.y;
      // 转换为度
      const angle = Math.atan2(deltaY, deltaX) * (180 / Math.PI);
      const angleTofixed = angle === 0 ? 0 : -angle.toFixed(1);
      outgoingAngleList.push({
        edgeId: item.id,
        angle: Number(angleTofixed),
      });
    });
  }
  const incomingToOutgoing: IncomeAndOutgo[] = [];
  incomingAngleList.forEach((item: any) => {
    outgoingAngleList.forEach((itemTwo: any) => {
      // 计算输入边到输出边的的角度值
      const angleDifference = calculateAngleDifference(item.angle, itemTwo.angle);
      incomingToOutgoing.push({
        angle: angleDifference,
        incomingEdgeId: item.edgeId,
        outgoingEdgeId: itemTwo.edgeId,
      });
    });
  });
  console.log(incomingToOutgoing, "计算输入边到输出边的的角度值");
  return incomingToOutgoing;
};

/**
 * 输入的角度到输出的角度差值
 * @param { number } inputAngle 输入的角度
 * @param { number } outputAngle 输出的角度
 */
export const calculateAngleDifference = (inputAngle: number, outputAngle: number) => {
  // 先确保角度在 0 到 360 度之间
  const normalizedInput = ((inputAngle % 360) + 360) % 360;
  const normalizedOutput = ((outputAngle % 360) + 360) % 360;
  // 计算角度差
  let angleDifference = normalizedOutput - normalizedInput;
  // 确保差值在 0 到 360 度之间
  if (angleDifference < 0) {
    angleDifference += 360;
  }
  // 确保在一个方向，取最小的角度的那边
  let angleDif: number = 0;
  if (angleDifference > 180) {
    angleDif = 360 - angleDifference - 3;
  } else {
    angleDif = angleDifference + 3;
  }
  const angleDifferenceSum = Number(angleDif.toFixed(0));
  return angleDifferenceSum;
};

export { getIncomingToOutgoing };
